<?php
/**
 * Created by PhpStorm.
 * User: 蒙明然
 * Date: 2017/9/7
 * Time: 15:52
 */

namespace app\admin\controller;

use think\Cache;
use think\Controller;
use think\config;
use app\common\model\Parts as PartModel;

class Parts extends Base
{
    public function index(){
        Cache::clear();
        $m_part = new PartModel();
        $key = input('key'); //搜索条件
        $map = [];
        if($key&&$key!==""){
            $map['parts_name'] = ['like',"%" . $key . "%"];
        }
        $group = new PartModel();
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('pagesize');
        $count = $group->getAllCount($map);         //获取总条数
        $allpage = intval(ceil($count / $limits));  //计算总页面
        $lists = $group->getAll($map, $Nowpage, $limits);
        foreach ($lists as $k=>$v){//添加分类信息
            $id['id']=$v['type_id'];
            $info=$group->getOneInfo($id);
            if(empty($info)){
                $lists[$k]['info']='暂未分类';
            }else{$lists[$k]['info']=$info['name'];}
        }
        $tlist=$m_part->getAllType();
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('tlist', $tlist);
        $this->assign('vals', $key);
        if(input('get.page')){
            return json($lists);
        }
        return $this->fetch();
    }

    /**
     * 添加设备
     */
    public function add(){
        Cache::clear();
        $m_part = new PartModel();
        if(request()->isPost()){
            $param = $this->request->post();
            $res = $m_part->insertOne($param);
            if($res){
                return json(['code'=>1,'msg'=>'添加成功','data'=>'']);
            }else{
                return json(['code'=>2,'msg'=>'添加失败','data'=>'']);
            }
        }
        return $this->fetch();
    }
    //添加类型
    public function typeAdd(){
        Cache::clear();
        $m_part = new PartModel();
        if(request()->isPost()){
            $param = $this->request->post();
            $res = $m_part->insertType($param);
            if($res){
                return json(['code'=>1,'msg'=>'添加成功','data'=>'']);
            }else{
                return json(['code'=>2,'msg'=>'添加失败','data'=>'']);
            }
        }
        return $this->fetch();
    }
    /**
     * 编辑设备
     */
    public function update(){
        Cache::clear();
        $m_part = new PartModel();
        $param = $this->request->post();
        $id = input("param.id");
        $where['parts_id']=$id;
        //查询用户信息
        $data = $m_part->selectOne($where);
        $tlist=$m_part->getAllType();
        $this->assign('res',$data);
        $this->assign('tlist',$tlist);
        return $this->fetch();
    }
    public function doupdate(){
        Cache::clear();
        $m_part = new PartModel();
        if(request()->isPost()){
            $data = $this->request->post();
            $where['parts_id']=$data['parts_id'];
            $res = $m_part->updateOne($where,$data);
            if($res){
                return $this->success('更新成功！','Parts/index');
            }else{
                return $this->error('更新失败！','Parts/index');
            }
        }
    }
    /**
     * 删除设备
     */
    public function del(){
        Cache::clear();
        $m_part = new PartModel();
        $id = input('param.id');
        $res = $m_part->deleteOne($id);
        if($res){
            return json(['code'=>1,'msg'=>'删除成功','data'=>'']);
        }else{
            return json(['code'=>1,'msg'=>'删除失败','data'=>'']);
        }
    }

    public function count(){
        $key = input('post.key');
        $mouth = input('post.mouth');
        switch ($mouth){
            case 0 :
                $begin = date('Y-m-01', strtotime(date("Y-m-d")));
                $end = date('Y-m-d', strtotime("$begin +1 month -1 day"));
                break;
            case 1 :
                $begin =  date('Y-m-d', strtotime(date('Y-m-01') . ' -1 month'));
                $end = date('Y-m-d', strtotime(date('Y-m-01') . ' -1 day'));
                break;
            default :
                $begin = '1999-1-1';
                $end = date('Y-m-d',time());
        }

        $where['a.is_delete'] = 0;
        $where['c.parts_id'] = ['neq','NULL'];
        $order = db('work_order')->alias('a')
            ->where($where)
            ->join('work_parts at', 'a.work_order_id = at.work_order_id')
            ->join('parts c','at.parts_id=c.parts_id')
            ->join('engineers d','a.repair_id=d.engineers_id')
            ->field('at.parts_number,c.parts_id,c.parts_name,d.engineers_id,d.engineers_name')
            ->whereTime('a.reception_time', 'between', [$begin, $end])
            ->order('c.parts_id')
            ->select();
        $this->assign('key', $key);
        $this->assign('mouth', $mouth);
        $this->assign('data',$order);
        return $this->fetch();
    }

}